local sk__shemi = fk.CreateSkill {

  name = "sk__shemi",

  tags = { Skill.Compulsory, },

}



sk__shemi:addEffect(fk.EventPhaseStart, {
  name = "sk__shemi",
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(sk__shemi.name) and player.phase == Player.Discard then
      return not player:isKongcheng()
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n = player:getHandcardNum() - player:getMaxCards()
    room:askForDiscard(player, math.max(n, 0), player:getHandcardNum(), false, sk__shemi.name, false, ".",
      "#sk__shemi:::" .. math.max(n, 0) .. ":" .. player:getMark(sk__shemi.name))
  end,
})
sk__shemi:addEffect(fk.EventPhaseEnd, {
  name = "sk__shemi",
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(sk__shemi.name) and player.phase == Player.Discard then
      return player:getMark("sk__shemi_invoke-phase") > 0
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(sk__shemi.name)
    room:notifySkillInvoked(player, sk__shemi.name, "special")
    room:addPlayerMark(player, MarkEnum.SkDrawNCards, 1)
    if not player:isWounded() then
      room:changeMaxHp(player, 1)
    end
  end,
})

sk__shemi:addAcquireEffect(function(self, player, is_start)
  local room = player.room
  local id, end_id = 1, 1
  room.logic:getEventsByRule(GameEvent.Phase, 1, function(e)
    if e.data.who == player and e.data.phase == Player.Discard then
      id, end_id = e.id, e.end_id
      return true
    end
  end, 1)
  if id > 1 then
    local n = 0
    room.logic:getEventsByRule(GameEvent.MoveCards, 1, function(e)
      if e.id <= end_id then
        for _, move in ipairs(e.data) do
          if move.from == player and move.moveReason == fk.ReasonDiscard and move.toArea == Card.DiscardPile then
            n = n + #move.moveInfo
          end
        end
      end
    end, id)
    room:setPlayerMark(player, sk__shemi.name, n)
  end
end)

sk__shemi:addEffect(fk.EventPhaseEnd, {
  can_refresh = function(self, event, target, player, data)
    if target == player then
      return player:hasSkill(self, true) and player.phase == Player.Discard
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local n = 0
    room.logic:getEventsByRule(GameEvent.MoveCards, 1, function(e)
      for _, move in ipairs(e.data) do
        if move.from == player and move.moveReason == fk.ReasonDiscard and move.toArea == Card.DiscardPile then
          n = n + #move.moveInfo
        end
      end
      return false
    end, room.logic:getCurrentEvent():findParent(GameEvent.Phase, true).id)
    if n > player:getMark(sk__shemi.name) then
      room:setPlayerMark(player, "sk__shemi_invoke-phase", 1)
    end
    room:setPlayerMark(player, sk__shemi.name, n)
  end,
})

return sk__shemi
